/**
 * User: alesj
 * Date: 18.3.2006
 * Time: 15:35:52
 * 
 * (C) Genera Lynx d.o.o.
 */

package com.alesj.blade.login;

import org.jboss.seam.annotations.Around;
import org.jboss.seam.annotations.Within;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.interceptors.*;

import javax.ejb.AroundInvoke;
import javax.ejb.InvocationContext;
import java.util.Arrays;

@Around({BijectionInterceptor.class, ValidationInterceptor.class,
        ConversationInterceptor.class, BusinessProcessInterceptor.class})
@Within(RemoveInterceptor.class)
public class LoginIfInterceptor {

    public static final String LOGIN_KEY = "loggedIn";

    @AroundInvoke
    public Object loginIf(InvocationContext invocation) throws Exception {
        Object result = invocation.proceed();
        LoginIf ann = invocation.getMethod().getAnnotation(LoginIf.class);
        if (ann != null) {
            if (Arrays.asList(ann.outcome()).contains(result)) {
                Contexts.getSessionContext().set(LOGIN_KEY, true);
            }
        }
        return result;
    }

}
